/*
 * Copyright 2006-2023 ahtelit.com, All Rights Reserved.
 */

package com.telit.android.service.impl;

import com.alibaba.excel.EasyExcel;
import com.telit.android.listener.excel.EasyExcelConsumerListener;
import com.telit.android.service.ImportService;
import com.telit.firecommand.model.easyExcel.FirePointExcel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * ImportServiceImpl
 *
 * @author yuezhuang
 * @since 2023-03-15
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ImportServiceImpl implements ImportService {

    @Override
    public List<List<String>> getDataFormExcel(MultipartFile file) throws IOException {
        // 获取监听对象
        EasyExcelConsumerListener<FirePointExcel> firePointReaderListener = new EasyExcelConsumerListener<>();
        // 读取文件
        EasyExcel.read(file.getInputStream(), FirePointExcel.class, firePointReaderListener).sheet().doRead();
        // 获取文件数组
        List<FirePointExcel> list = firePointReaderListener.getDatas();
        // 将集合对象转成集合数组
        List<List<String>> resultList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(firePointExcel -> {
                List<String> simpleList = new ArrayList<>();
                simpleList.add(firePointExcel.getLongitude());
                simpleList.add(firePointExcel.getLatitude());
                resultList.add(simpleList);
            });
        }
        return resultList;
    }

}
